```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>最长公共前缀 - 算法可视化</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background-color: #f8f9fa;
            color: #333;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
        }
        .code-block {
            background-color: #2d3748;
            border-left: 4px solid #4299e1;
        }
        .algorithm-step {
            transition: all 0.3s ease;
        }
        .algorithm-step:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
        }
        .highlight {
            position: relative;
        }
        .highlight::after {
            content: '';
            position: absolute;
            left: 0;
            bottom: 2px;
            width: 100%;
            height: 6px;
            background-color: rgba(66, 153, 225, 0.3);
            z-index: -1;
            transition: all 0.3s ease;
        }
        .hover-grow {
            transition: transform 0.3s ease;
        }
        .hover-grow:hover {
            transform: scale(1.02);
        }
    </style>
</head>
<body class="antialiased">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl px-4">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-1/2 mb-10 md:mb-0">
                    <h1 class="text-4xl md:text-5xl font-bold mb-4 font-serif">最长公共前缀</h1>
                    <p class="text-xl md:text-2xl mb-6 opacity-90">探索字符串数组中的共性之美</p>
                    <p class="text-lg opacity-80 mb-8">一个看似简单的算法问题，却蕴含着字符串处理的精髓。让我们深入剖析这个优雅的解决方案，理解其背后的逻辑和实现细节。</p>
                    <div class="flex space-x-4">
                        <button class="bg-white text-indigo-700 hover:bg-indigo-100 px-6 py-3 rounded-lg font-medium transition duration-300 transform hover:scale-105">
                            <i class="fas fa-play mr-2"></i>可视化演示
                        </button>
                        <button class="bg-transparent border-2 border-white hover:bg-white hover:text-indigo-700 px-6 py-3 rounded-lg font-medium transition duration-300 transform hover:scale-105">
                            <i class="fas fa-code mr-2"></i>查看代码
                        </button>
                    </div>
                </div>
                <div class="md:w-1/2 flex justify-center">
                    <div class="relative w-full max-w-md">
                        <div class="absolute top-0 left-0 w-full h-full bg-indigo-500 rounded-xl opacity-20 transform rotate-6"></div>
                        <div class="absolute top-0 left-0 w-full h-full bg-blue-500 rounded-xl opacity-20 transform -rotate-6"></div>
                        <div class="relative bg-white text-gray-800 p-6 rounded-xl shadow-2xl">
                            <div class="text-sm font-mono mb-4 text-indigo-600">输入示例：</div>
                            <div class="mb-2 flex items-center">
                                <span class="bg-indigo-100 text-indigo-800 px-2 py-1 rounded mr-2">strs[0]</span>
                                <span class="font-medium">"flower"</span>
                            </div>
                            <div class="mb-2 flex items-center">
                                <span class="bg-indigo-100 text-indigo-800 px-2 py-1 rounded mr-2">strs[1]</span>
                                <span class="font-medium">"flow"</span>
                            </div>
                            <div class="mb-4 flex items-center">
                                <span class="bg-indigo-100 text-indigo-800 px-2 py-1 rounded mr-2">strs[2]</span>
                                <span class="font-medium">"flight"</span>
                            </div>
                            <div class="border-t pt-4">
                                <div class="text-sm font-mono mb-2 text-indigo-600">输出结果：</div>
                                <div class="text-xl font-bold">"fl"</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <div class="container mx-auto max-w-5xl px-4 py-16">
        <!-- Problem Section -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <div class="w-12 h-12 rounded-full bg-indigo-100 flex items-center justify-center mr-4">
                    <i class="fas fa-question text-indigo-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold font-serif">题目描述</h2>
            </div>
            <div class="bg-white rounded-xl shadow-md p-8 hover-grow">
                <p class="text-lg leading-relaxed mb-6">编写一个函数来查找字符串数组中的最长公共前缀。如果不存在公共前缀，则返回空字符串 <code class="bg-gray-100 px-2 py-1 rounded">""</code>。</p>
                
                <div class="grid md:grid-cols-2 gap-8 mt-8">
                    <div>
                        <h3 class="text-xl font-semibold mb-4 text-indigo-700 flex items-center">
                            <i class="fas fa-lightbulb mr-2"></i> 示例 1
                        </h3>
                        <div class="bg-gray-50 p-4 rounded-lg">
                            <p class="font-medium mb-2">输入: <code class="bg-gray-100 px-2 py-1 rounded">strs = ["flower","flow","flight"]</code></p>
                            <p class="font-medium">输出: <code class="bg-gray-100 px-2 py-1 rounded">"fl"</code></p>
                        </div>
                    </div>
                    <div>
                        <h3 class="text-xl font-semibold mb-4 text-indigo-700 flex items-center">
                            <i class="fas fa-lightbulb mr-2"></i> 示例 2
                        </h3>
                        <div class="bg-gray-50 p-4 rounded-lg">
                            <p class="font-medium mb-2">输入: <code class="bg-gray-100 px-2 py-1 rounded">strs = ["dog","racecar","car"]</code></p>
                            <p class="font-medium">输出: <code class="bg-gray-100 px-2 py-1 rounded">""</code></p>
                            <p class="text-sm text-gray-500 mt-2">解释: 输入不存在公共前缀</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Solution Section -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <div class="w-12 h-12 rounded-full bg-blue-100 flex items-center justify-center mr-4">
                    <i class="fas fa-code text-blue-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold font-serif">解决方案</h2>
            </div>
            
            <div class="grid md:grid-cols-3 gap-8">
                <!-- Algorithm Card -->
                <div class="algorithm-step bg-white rounded-xl shadow-md p-6 hover-grow">
                    <div class="text-blue-500 text-2xl mb-4">
                        <i class="fas fa-project-diagram"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3 highlight">核心算法</h3>
                    <p class="text-gray-600 mb-4">字符串比较：以第一个字符串为基准，逐个比较其他字符串的对应位置。</p>
                    <div class="text-sm bg-blue-50 text-blue-800 p-3 rounded-lg">
                        <p class="font-medium">时间复杂度: O(S)</p>
                        <p>其中S为所有字符串的总长度</p>
                    </div>
                </div>
                
                <!-- Steps Card -->
                <div class="algorithm-step bg-white rounded-xl shadow-md p-6 hover-grow">
                    <div class="text-purple-500 text-2xl mb-4">
                        <i class="fas fa-list-ol"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3 highlight">解题步骤</h3>
                    <ol class="list-decimal list-inside space-y-2 text-gray-600">
                        <li>以第一个字符串作为初始公共前缀</li>
                        <li>遍历后续每个字符串</li>
                        <li>逐步缩短前缀直到匹配当前字符串</li>
                        <li>如果前缀为空则立即返回</li>
                    </ol>
                </div>
                
                <!-- Visualization Card -->
                <div class="algorithm-step bg-white rounded-xl shadow-md p-6 hover-grow">
                    <div class="text-green-500 text-2xl mb-4">
                        <i class="fas fa-chart-line"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3 highlight">可视化思路</h3>
                    <div class="text-gray-600 mb-4">
                        <p>通过逐步比较和缩短前缀的过程，直观展示算法的执行流程。</p>
                    </div>
                    <div id="mermaid-diagram" class="mermaid">
                        graph TD
                            A[初始前缀: flower] --> B[比较 flow]
                            B --> C[缩短为 flow]
                            C --> D[比较 flight]
                            D --> E[缩短为 fl]
                            E --> F[完成]
                    </div>
                </div>
            </div>
        </section>

        <!-- Code Section -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <div class="w-12 h-12 rounded-full bg-green-100 flex items-center justify-center mr-4">
                    <i class="fas fa-terminal text-green-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold font-serif">示例代码</h2>
            </div>
            
            <div class="code-block text-gray-100 rounded-lg overflow-hidden shadow-xl hover-grow">
                <div class="flex items-center bg-gray-800 px-4 py-3">
                    <div class="flex space-x-2 mr-4">
                        <span class="w-3 h-3 rounded-full bg-red-500"></span>
                        <span class="w-3 h-3 rounded-full bg-yellow-500"></span>
                        <span class="w-3 h-3 rounded-full bg-green-500"></span>
                    </div>
                    <div class="text-sm">longest_common_prefix.py</div>
                </div>
                <pre class="p-6 overflow-x-auto"><code class="language-python">def longestCommonPrefix(strs):
    if not strs:
        return ""
    # 以第一个字符串为基准
    prefix = strs[0]
    
    # 遍历后续字符串
    for string in strs[1:]:
        # 逐步缩短前缀直到匹配当前字符串
        while string[:len(prefix)] != prefix:
            prefix = prefix[:-1]
            # 如果前缀为空则立即返回
            if not prefix:
                return ""
    
    return prefix</code></pre>
            </div>
            
            <div class="mt-8 grid md:grid-cols-2 gap-8">
                <div class="bg-white rounded-xl shadow-md p-6 hover-grow">
                    <h3 class="text-xl font-semibold mb-4 text-indigo-700 flex items-center">
                        <i class="fas fa-check-circle mr-2"></i> 边界条件处理
                    </h3>
                    <ul class="list-disc list-inside space-y-2 text-gray-600">
                        <li>空输入数组直接返回空字符串</li>
                        <li>当公共前缀缩减为空时立即终止</li>
                        <li>处理数组中任意空字符串的情况</li>
                    </ul>
                </div>
                <div class="bg-white rounded-xl shadow-md p-6 hover-grow">
                    <h3 class="text-xl font-semibold mb-4 text-indigo-700 flex items-center">
                        <i class="fas fa-rocket mr-2"></i> 算法优化
                    </h3>
                    <ul class="list-disc list-inside space-y-2 text-gray-600">
                        <li>时间复杂度已最优 - O(S)</li>
                        <li>空间复杂度 O(1) - 只使用常数额外空间</li>
                        <li>提前终止机制提高实际运行效率</li>
                    </ul>
                </div>
            </div>
        </section>

        <!-- Interactive Demo -->
        <section>
            <div class="flex items-center mb-8">
                <div class="w-12 h-12 rounded-full bg-yellow-100 flex items-center justify-center mr-4">
                    <i class="fas fa-sliders-h text-yellow-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold font-serif">交互式演示</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden hover-grow">
                <div class="p-6 border-b">
                    <h3 class="text-xl font-semibold mb-4">输入测试用例</h3>
                    <div class="flex flex-col md:flex-row md:items-end gap-4">
                        <div class="flex-1">
                            <label class="block text-sm font-medium text-gray-700 mb-1">输入字符串数组 (JSON格式)</label>
                            <input type="text" class="w-full px-4 py-2 border rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500" value='["flower","flow","flight"]' id="input-array">
                        </div>
                        <button class="bg-indigo-600 text-white px-6 py-2 rounded-lg font-medium hover:bg-indigo-700 transition duration-300 transform hover:scale-105" id="run-demo">
                            <i class="fas fa-play mr-2"></i>运行演示
                        </button>
                    </div>
                </div>
                
                <div class="p-6 bg-gray-50">
                    <h3 class="text-xl font-semibold mb-4">执行过程可视化</h3>
                    <div class="flex justify-center my-8">
                        <div class="relative w-full max-w-2xl">
                            <div class="absolute top-0 left-0 w-full h-full bg-indigo-200 rounded-lg opacity-20 transform rotate-1"></div>
                            <div class="relative bg-white p-6 rounded-lg shadow-sm border border-gray-200">
                                <div class="flex justify-between items-center mb-6">
                                    <div class="text-lg font-medium">当前状态:</div>
                                    <div class="px-3 py-1 bg-indigo-100 text-indigo-800 rounded-full text-sm font-medium" id="status">等待执行</div>
                                </div>
                                
                                <div class="space-y-4">
                                    <div>
                                        <div class="text-sm text-gray-500 mb-1">初始前缀:</div>
                                        <div class="bg-gray-100 p-3 rounded" id="initial-prefix">-</div>
                                    </div>
                                    
                                    <div>
                                        <div class="text-sm text-gray-500 mb-1">当前比较字符串:</div>
                                        <div class="bg-gray-100 p-3 rounded" id="current-string">-</div>
                                    </div>
                                    
                                    <div>
                                        <div class="text-sm text-gray-500 mb-1">操作:</div>
                                        <div class="bg-gray-100 p-3 rounded" id="operation">-</div>
                                    </div>
                                    
                                    <div>
                                        <div class="text-sm text-gray-500 mb-1">更新后的前缀:</div>
                                        <div class="bg-gray-100 p-3 rounded" id="updated-prefix">-</div>
                                    </div>
                                </div>
                                
                                <div class="mt-6 pt-6 border-t">
                                    <div class="text-lg font-medium mb-2">最终结果:</div>
                                    <div class="text-2xl font-bold text-indigo-600" id="final-result">-</div>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="mt-8">
                        <h4 class="text-lg font-medium mb-4">逐步执行:</h4>
                        <div class="flex space-x-4 overflow-x-auto pb-4" id="step-indicators">
                            <!-- 步骤指示器将通过JS动态生成 -->
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </div>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
        
        document.addEventListener('DOMContentLoaded', function() {
            const runDemoButton = document.getElementById('run-demo');
            const inputArray = document.getElementById('input-array');
            
            runDemoButton.addEventListener('click', function() {
                try {
                    const strs = JSON.parse(inputArray.value);
                    if (!Array.isArray(strs) || strs.some(s => typeof s !== 'string')) {
                        throw new Error('请输入有效的字符串数组');
                    }
                    
                    simulateLongestCommonPrefix(strs);
                } catch (e) {
                    alert('输入无效: ' + e.message);
                }
            });
            
            function simulateLongestCommonPrefix(strs) {
                // 重置UI
                document.getElementById('status').textContent = '执行中...';
                document.getElementById('status').className = 'px-3 py-1 bg-yellow-100 text-yellow-800 rounded-full text-sm font-medium';
                document.getElementById('initial-prefix').textContent = '-';
                document.getElementById('current-string').textContent = '-';
                document.getElementById('operation').textContent = '-';
                document.getElementById('updated-prefix').textContent = '-';
                document.getElementById('final-result').textContent = '-';
                document.getElementById('step-indicators').innerHTML = '';
                
                if (strs.length === 0) {
                    updateFinalResult('');
                    return;
                }
                
                let prefix = strs[0];
                updateInitialPrefix(prefix);
                
                // 创建步骤指示器
                const stepIndicators = document.getElementById('step-indicators');
                let stepIndex = 0;
                
                // 模拟执行过程
                let i = 1;
                const interval = setInterval(() => {
                    if (i >= strs.length) {
                        clearInterval(interval);
                        updateFinalResult(prefix);
                        return;
                    }
                    
                    const currentString = strs[i];
                    updateCurrentString(currentString);
                    
                    // 添加步骤指示器
                    const stepDiv = document.createElement('div');
                    stepDiv.className = 'flex-shrink-0 bg-indigo-100 text-indigo-800 px-4 py-2 rounded-lg cursor-pointer';
                    stepDiv.textContent = `步骤 ${stepIndex + 1}`;
                    stepDiv.addEventListener('click', () => showStepDetails(stepIndex));
                    stepIndicators.appendChild(stepDiv);
                    stepIndex++;
                    
                    // 模拟缩短前缀的过程
                    let j = 0;
                    const shortenInterval = setInterval(() => {
                        if (currentString.indexOf(prefix) !== 0) {
                            prefix = prefix.slice(0, -1);
                            updateOperation(`缩短前缀: 移除最后一个字符`);
                            updateUpdatedPrefix(prefix);
                            
                            if (prefix === '') {
                                clearInterval(shortenInterval);
                                i = strs.length; // 提前终止
                                setTimeout(() => {
                                    updateFinalResult('');
                                }, 1000);
                            }
                        } else {
                            clearInterval(shortenInterval);
                            updateOperation(`前缀匹配成功`);
                            updateUpdatedPrefix(prefix);
                            i++;
                        }
                    }, 800);
                    
                }, 2000);
                
                function updateInitialPrefix(p) {
                    document.getElementById('initial-prefix').textContent = p || '""';
                }
                
                function updateCurrentString(s) {
                    document.getElementById('current-string').textContent = s;
                }
                
                function updateOperation(op) {
                    document.getElementById('operation').textContent = op;
                }
                
                function updateUpdatedPrefix(p) {
                    document.getElementById('updated-prefix').textContent = p || '""';
                }
                
                function updateFinalResult(result) {
                    document.getElementById('status').textContent = '执行完成';
                    document.getElementById('status').className = 'px-3 py-1 bg-green-100 text-green-800 rounded-full text-sm font-medium';
                    document.getElementById('final-result').textContent = result || '""';
                }
                
                function showStepDetails(step) {
                    // 这里可以实现点击步骤查看详细信息的逻辑
                    console.log('Showing details for step', step);
                }
            }
        });
    </script>
</body>
</html>
```